<?php
class Users extends MY_Controller{
	var $beforeFilter = array(
			'initAdmin' => array(
				'only' => array('admin','add','remove','edit','save'),
			),
			'authorize' => array(
				'only' => array('changepwd'),
			),
			'getAllRoles' => array(
				'only' => array('add','edit'),
			),
		);

	function __construct()
	{
		parent::MY_Controller();
		$this->load->model('User');
		$this->load->helper('cookie');
	}
	
	function index()
	{
		
	}

	function login(){
		if(!empty($_POST)){
			$data = $_POST;
			$user = $this->User->doLogin($data['username'],$data['password']);
			if($user!==false){
				$newdata['id']		= $user['id'];
				$newdata['truename']	= $user['truename'];
				$newdata['username']	= $user['username'];
				if(!empty($user['roles'])) $newdata['roles']	= $user['roles']==1?1:$user['roles'];
				if($data['rememberme']=='forever'){
					$this->config->set_item('sess_expiration', 0);
				}
				
				$this->session->set_userdata($newdata);
				$redirect = $this->getRedirect("/index.php/admin");
				$key = $this->config->item('system_encode_key');
				$mes = $this->lang->line('mes_success');
				$u = $data['username'];
				$p = $data['password'];
				$this->showMessage($mes,$redirect);
			}else{
				$this->showMessage($this->User->_error);
			}
		}
		$this->display();
	}

	function logout(){
		$this->load->library('session');
		$this->session->sess_destroy();
		$redirect = $this->getRedirect("user_home");
		$forum_url = sprintf(base_url()."forum/ucp.php?mode=logout&sid=%s",get_cookie('session_id'));
		$mes = $this->lang->line('mes_success');
		$this->showMessage($mes,$redirect);
	}
	

	function admin(){
		$like = $this->_searchLike($this->User);
		$where = $this->_searchWhere($this->User);
		$map = array();
		if(!empty($like)) $map['like'] = $like;
		if(!empty($where)) $map['where'] = $where;
		$this->assign['rs'] = $this->_list($this->User,$map);
		$this->template = "users_admin";
		$this->display();
	}

	function save(){
		$redirect = $this->getRedirect('user_admin');
		if($_POST['oldusername']!=$this->session->userdata('username') ){
			if( intval($this->session->userdata('roles'))!=1 ){
				$mes = 'no_roles';
				$this->showMessage($mes,$redirect);
			}
		}
		$rules['username']	= "trim|required|min_length[3]|max_length[35]";
		$rules['password']	= "trim|required|matches[passwordConfirm]|min_length[5]|max_length[12]";
		if($_POST['id']>0&&empty($_POST['password'])){
			unset($rules['password']);
			unset($_POST['password']);
		}
		$rules['email']	= "trim|required|valid_email";
		if ($this->validation($rules,'user') == FALSE)
		{
			$this->showMessage($this->validation->error_string); 
		}
		if(($user_id = $this->User->save($_POST))!==false){
			$mes = 'mes_success';
			$this->showMessage($mes,$redirect);
		}else{
			$this->showMessage($this->User->_error); 
		}
	}
	

	function add(){
		$this->template = "users_save";
		$this->display();
	}

	function edit($userId){
		$res = $this->User->findByUserId($userId);
		$this->assign['user'] = $res;
		$userRoles = array();
		$this->template = "users_save";
		$this->display();
	}

	function remove(){
		$this->display();
	}

}